/* eslint-disable no-param-reassign */

let id = 0;

const addIds = (jsonConfig, startAt = 0) => {
  function _addIds(config) {
    if (!config) return

    config.internalId = startAt
    startAt += 1
    id = startAt;
    if (!config.children) return
    config.children.forEach(el => {
      _addIds(el)
    })
  }

  return _addIds(jsonConfig)
}

const getNewId = () => {
  return id++;
}

const removeIds = jsonConfig => {
  function _removeIds(config) {
    if (!config) return

    delete config.internalId

    if (!config.children) return
    config.children.forEach(el => {
      _removeIds(el)
    })
  }

  return _removeIds(jsonConfig)
}

export default {
  addIds,
  removeIds,
  getNewId
}
